<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@include file="common.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>物料需求</title>
    <script type="text/javascript" src="${pageContext.request.contextPath }/js/bootstrapValidator.min.js"></script>
    <link href="${pageContext.request.contextPath }/css/bootstrapValidator.min.css">
    <script type="text/javascript">
        $(function () {
            $(".form_datetime").datetimepicker({
                format: "yyyy-mm-dd",
                autoclose: true,
                todayBtn: true,
                todayHighlight: true,
                showMeridian: true,
                pickerPosition: "bottom-left",
                language: 'zh-CN',//中文，需要引用zh-CN.js包
                startView: 2,//月视图
                minView: 2//日期时间选择器所能够提供的最精确的时间选择视图
            });

            $('#usertype').selectpicker({
                'selectedText': 'cat'
            });


            $.ajax({
                url: '${pageContext.request.contextPath }/PlanDetailed/supperAdd.do',
                type: "POST",
                dataType: 'json',
                success: function (result) {

                    for (var i = 0; i < result.length; i++) {
                        $(".txt_search_supper").append("<option value=" + result[i].supplierid + ">" + result[i].suppliername + "</option>");
                    }
                }
            });

        });
    </script>
    <script type="text/javascript">
        $(function () {

            //1.初始化Table
            var oTable = new TableInit();
            oTable.Init();

            //2.初始化Button的点击事件
            var oButtonInit = new ButtonInit();
            oButtonInit.Init();

            toastr.options.positionClass = 'toast-top-center';

            $('#btn_add').click(function () {

                /*$(':input', '#sfm')
                    .not(':button, :submit, :reset, :hidden')
                    .val('')
                    .removeAttr('checked')
                    .removeAttr('selected');*/

                /*$('#sfm').data('bootstrapValidator').destroy();
                $('#sfm').data('bootstrapValidator', null);
                formValidator();*/


                $("#sfm")[0].reset();
                $('#sfm').data('bootstrapValidator').resetForm(true);

                $('#sfm').data('bootstrapValidator').destroy();
                $('#sfm').data('bootstrapValidator', null);
                formValidator();

                $('#myModalInsert').modal("show");

            });


            /**
             * 条件搜索
             */
            $('#query').click(function () {
                $('#tb_tableDemand').bootstrapTable('refresh');

            });

            /**
             * 选中行,颜色变化
             */
            /*$('#tb_tableDemand').on('click-row.bs.table', function (e, row, element) {

                $('.danger').removeClass('danger');//去除之前选中的行的，选中样式
                $(element).addClass('danger');//添加当前选中的 success样式用于区别
            })*/

            /**
             * 物料删除
             */
            $('#btn_delete').click(function () {
                var row = $("#tb_tableDemand").bootstrapTable('getSelections');
                if (row.length == 0) {

                    toastr.warning('请选择有效数据');

                    return;
                }

                var ids = [];
                for (var i = 0; i < row.length; i++) {

                    ids.push(row[i].id);

                }

                Ewin.confirm({message: "确认要删除选择的数据吗？"}).on(function (e) {

                    if (!e) {
                        $('#tb_tableDemand').bootstrapTable('refresh');
                        return;
                    }

                    $.ajax({
                        type: "POST",
                        url: '${pageContext.request.contextPath }/materie/materieDelete.do',
                        data: 'ids=' + ids,
                        success: function (result) {
                            var result = eval("(" + result + ")");
                            if (result.success) {
                                toastr.success("删除成功");

                              setTimeout(function () {
                                  $('#tb_tableDemand').bootstrapTable('refresh');

                                  setTimeout(function () {
                                      var row = $('#tb_tableDemand').bootstrapTable('getData');

                                      if (row.length == 0) {
                                          window.location.reload();
                                      }
                                  },1000);
                              },1000);




                            } else {
                                toastr.error('删除失败!');
                            }
                        }
                    });
                });
            });


            /**
             * 将修改的信息带回
             */
            $('#btn_edit').click(function () {

                /**
                 * 验证方法
                 */
                $("#update_fm").bootstrapValidator({
                    message: '输入值不合法',
                    feedbackIcons: {
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                    },
                    fields: {
                        name: {
                            message: '用户名不合法',
                            validators: {
                                notEmpty: {
                                    message: '物料名称不能为空'
                                },
                                stringLength: {
                                    min: 1,
                                    max: 30,
                                    message: '请输入1到30个字符'
                                },
                            }
                        }, materialmodel: {
                            message: '用户名不合法',
                            validators: {
                                notEmpty: {
                                    message: '物料型号不能为空'
                                },
                                stringLength: {
                                    min: 1,
                                    max: 30,
                                    message: '请输入1到30个字符'
                                },
                                callback: {
                                    message: '物料型号不能重复',
                                    callback:function(value, validator){

                                        var flg = false;

                                        $.ajax({
                                            type:'post',
                                            async:false,
                                            data:'materialModel='+value,
                                            url:'${pageContext.request.contextPath}/materie/materie_selectByMaterialmodel.do',
                                            success:function (data) {
                                                var result=eval("("+data+")");
                                                if(result.success){

                                                    flg  = true;

                                                }else if(result.success){

                                                    flg = false;

                                                }

                                                var ma = row[0].materialmodel;


                                                if (ma == value) {
                                                    flg  = true;
                                                }
                                            }
                                        });

                                        return flg;


                                    }
                                },
                            }
                        /*},  remark1: {
                            validators: {
                                notEmpty: {
                                    message: '包装规格不能为空'
                                },
                            } */
                        }, remark3: {
                            validators: {
                                /* notEmpty: {
                                    message: '单价不能为空'
                                }, */
                                regexp: {
                                    regexp: '^(0|[1-9][0-9]{0,9})(\.[0-9]{1,2})?$',
                                    message: '格式不正确'
                                }
                            }
                        }, remark4: {
                            validators: {
                                /* notEmpty: {
                                    message: '安全库存量不能为空'
                                }, */
                                regexp: {
                                    regexp: '^[0-9]*$',
                                    message: '安全库存量只能输入整数'
                                }
                            }
                        }, remark7: {
                            validators: {
                                notEmpty: {
                                    message: '安全库存量不能为空'
                                }, 
                                regexp: {
                                    regexp: '^[0-9]*$',
                                    message: '安全库存量只能输入整数'
                                }
                            }
                        }, remark8: {
                            validators: {
                               /*  notEmpty: {
                                    message: '安全库存量不能为空'
                                }, */
                                regexp: {
                                    regexp: '^[0-9]*$',
                                    message: '安全库存量只能输入整数'
                                }
                            }
                        }
                    }
                });


                /*$("#myModalupdate")[0].reset();
                $('#myModalupdate').data('bootstrapValidator').resetForm(true);*/

                var row = $("#tb_tableDemand").bootstrapTable('getSelections');
                if (row.length != 1) {
                    toastr.warning('请选择一条有效数据');
                    return;
                }



                $.ajax({
                    type: 'post',

                    data: 'id=' + row[0].id,
                    url: '${pageContext.request.contextPath }/materie/materie_selectById.do',
                    success: function (data) {
                        var result = eval("(" + data + ")");
                        //物料名称
                        $('input[name="name"]').val(result.name);
                        //物料型号
                        $('input[name="materialmodel"]').val(result.materialmodel);

                        //颜色
                        /*var selects = $('#colorUpdate').find("option");

                        for (var i = 0; i < selects.length; i++) {

                            if ($(selects[i]).val() == result.color) {

                                $(selects[i]).attr("selected", "selected");
                            }

                        }*/

                        $('#colorUpdate').val(result.color);

                        //包装规格
                        $('input[name="remark1"]').val(result.remark1);
                        //车种
                        $('input[name="remark2"]').val(result.remark2);
                        //计量单位
                        var colors = $('#compUpdate').find("option");

                        for (var j = 0; j < colors.length; j++) {
                            if ($(colors[j]).val() == result.company) {

                                $(colors[j]).attr("selected", true);
                            }

                        }

                        //单价
                        $('input[name="remark3"]').val(result.remark3);
                        //最低安全库存量
                        $('input[name="remark7"]').val(result.remark7);
                        //最高安全库存量
                        $('input[name="remark8"]').val(result.remark8);
                        //安全库存量
                        $('input[name="remark4"]').val(result.remark4);
                        //供应商
                        var suppers = $('#supperUpdate').find("option");

                        for (var z = 0; z < suppers.length; z++) {

                            if ($(suppers[z]).val() == result.supplierid) {
                                $(suppers[z]).attr("selected", true);
                            }

                        }
                        //主键id
                        $('input[name="id"]').val(result.id);
                    }
                })
                $('#myModalupdate').modal("show");


                $('#update_fm').data('bootstrapValidator').destroy();
                $('#update_fm').data('bootstrapValidator', null);




                /**
                 * 验证方法
                 */
                $("#update_fm").bootstrapValidator({
                    message: '输入值不合法',
                    feedbackIcons: {
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                    },
                    fields: {
                        name: {
                            message: '用户名不合法',
                            validators: {
                                notEmpty: {
                                    message: '物料名称不能为空'
                                },
                                stringLength: {
                                    min: 1,
                                    max: 30,
                                    message: '请输入1到30个字符'
                                },
                            }
                        }, materialmodel: {
                            message: '用户名不合法',
                            validators: {
                                notEmpty: {
                                    message: '物料型号不能为空'
                                },
                                stringLength: {
                                    min: 1,
                                    max: 30,
                                    message: '请输入1到30个字符'
                                },
                                callback: {
                                    message: '物料型号不能重复',
                                    callback:function(value, validator){

                                        var flg = false;

                                        $.ajax({
                                            type:'post',
                                            async:false,
                                            data:'materialModel='+value,
                                            url:'${pageContext.request.contextPath}/materie/materie_selectByMaterialmodel.do',
                                            success:function (data) {
                                                var result=eval("("+data+")");
                                                if(result.success){

                                                    flg  = true;

                                                }else if(result.success){

                                                    flg = false;

                                                }

                                                var ma = row[0].materialmodel;


                                                if (ma == value) {
                                                    flg  = true;
                                                }
                                            }
                                        });

                                        return flg;


                                    }
                                },
                            }
                      /*   }, remark1: {
                            validators: {
                                notEmpty: {
                                    message: '包装规格不能为空'
                                },
                            } */
                        }, remark3: {
                            validators: {
                               /*  notEmpty: {
                                    message: '单价不能为空'
                                }, */
                                regexp: {
                                    regexp: '^(0|[1-9][0-9]{0,9})(\.[0-9]{1,5})?$',
                                    message: '格式不正确'
                                }
                            }
                        }, remark4: {
                            validators: {
                               /*  notEmpty: {
                                    message: '安全库存量不能为空'
                                }, */
                                regexp: {
                                    regexp: '^[0-9]*$',
                                    message: '安全库存量只能输入整数'
                                }
                            }
                        }, remark7: {
                            validators: {
                               /*  notEmpty: {
                                    message: '安全库存量不能为空'
                                }, */
                                regexp: {
                                    regexp: '^[0-9]*$',
                                    message: '安全库存量只能输入整数'
                                }
                            }
                        }, remark8: {
                            validators: {
                              /*   notEmpty: {
                                    message: '安全库存量不能为空'
                                }, */
                                regexp: {
                                    regexp: '^[0-9]*$',
                                    message: '安全库存量只能输入整数'
                                }
                            }
                        }
                    }
                });



            });

            /**
             * 清空表单,将验证进行重置,调用验证
             */
            $("#btn_close").click(function () {

                clear();

            });

            $("#btn_out").click(function () {



                Ewin.confirm({message: "确认要导出所有数据吗？"}).on(function (e) {

                    if (e) {
                        window.location.href="${pageContext.request.contextPath}/materie/exportAll.do";
                        return;
                    }
                })



            });
            $("#btn_enter").click(function () {
            	
            	 
                $("#import").modal("show");
                document.getElementById("ffImport").reset();
            });
        });

        function Preview() {
            window.location.href="${pageContext.request.contextPath}/materie/downloadLocal.do";
        }


        function clear() {
            $(':input', '#sfm')
                .not(':button, :submit, :reset, :hidden')
                .val('')
                .removeAttr('checked')
                .removeAttr('selected');

            $('#sfm').data('bootstrapValidator').destroy();
            $('#sfm').data('bootstrapValidator', null);
            formValidator();
        }

        var TableInit = function () {
            var oTableInit = new Object();
            //初始化Table
            oTableInit.Init = function () {
                $('#tb_tableDemand').bootstrapTable({
                    url: '${pageContext.request.contextPath }/materie/materieList.do',
                    method: 'get',                      //请求方式（*）
                    toolbar: '#toolbars',                //工具按钮用哪个容器
                    striped: true,                      //是否显示行间隔色
                    cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                    pagination: true,                   //是否显示分页（*）
                    sortable: false,                     //是否启用排序
                    sortOrder: "asc",                   //排序方式
                    queryParams: oTableInit.queryParams,//传递参数（*）
                    sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
                    pageNumber: 1,                       //初始化加载第一页，默认第一页
                    pageSize: 10,                       //每页的记录行数（*）
                    pageList: [10, 20, 30],        //可供选择的每页的行数（*）
                    search: false,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
                    strictSearch: true,
                    showColumns: false,                  //是否显示所有的列
                    showRefresh: false,                  //是否显示刷新按钮
                    minimumCountColumns: 2,             //最少允许的列数
                    clickToSelect: true,                //是否启用点击选中行
                    height: 488,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                    uniqueId: "ID",                     //每一行的唯一标识，一般为主键列
                    showToggle: false,                    //是否显示详细视图和列表视图的切换按钮
                    cardView: false,                    //是否显示详细视图
                    detailView: false,
                    /*rowStyle: function (row, index) {
                        //这里有5个取值代表5中颜色['active', 'success', 'info', 'warning', 'danger'];
                        var strclass = "";
                        if (index % 2 == 1) {
                            var strclass = "success";
                        } else {
                            var strclass = "active";
                        }
                        return {
                            classes: strclass
                        }
                    },*/
                    columns: [{
                        checkbox: true
                    }, /*{
                        title: '序号',
                        formatter: function (value, row, index) {
                            return index + 1;
                        }
                    },*/ {
                        field: 'remark15',
                        title: '供应商',

                    }, {
                        field: 'remark2',
                        title: '车种'
                    }, {
                        field: 'name',
                        title: '物料名称'
                    }, {
                        field: 'materialmodel',
                        title: '物料型号'
                    }, {
                        field: 'color',
                        title: '颜色'
                    }, {
                        field: 'remark1',
                        title: '包装规格'
                    }, {
                        field: 'company',
                        title: '计量单位'
                    }, {
                        field: 'remark3',
                        title: '单价'
                    }, {
                        field: 'remark7',
                        title: '最低安全库存量'
                    }, {
                        field: 'remark8',
                        title: '最高安全库存量'
                    }]
                });
            };

            //得到查询的参数
            oTableInit.queryParams = function (params) {
                var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                    limit: params.limit,   //页面大小
                    offset: params.offset,  //页码
                    name: $("#txt_search_name").val(),
                    materialmodel: $("#txt_search_materialmodel").val(),
                };
                return temp;
            };
            return oTableInit;
        };


        var ButtonInit = function () {
            var oInit = new Object();
            var postdata = {};

            oInit.Init = function () {
                //初始化页面上面的按钮事件
            };

            return oInit;
        };

    </script>
</head>
<body>
<div class="panel-body" style="padding-bottom:0px;">
    <div class="panel panel-default">
        <div class="panel-heading">查询条件</div>
        <div class="panel-body">
            <form id="formSearch" class="form-horizontal">
                <div class="form-group" style="margin-top:15px">
                    <label class="control-label col-sm-1" for="txt_search_name">物料名称</label>
                    <div class="col-sm-2">
                        <input type="text" class="form-control" id="txt_search_name">
                    </div>
                    <label class="control-label col-sm-1" for="txt_search_materialmodel">物料型号</label>
                    <div class="col-sm-2">
                        <input type="text" class="form-control" id="txt_search_materialmodel">
                    </div>
                    <div class="col-sm-2" style="text-align:left;">
                        <!-- <button type="reset" style="margin-left:50px" class="btn btn-primary">清除</button> -->
                        <button type="button" style="margin-left:10px" id="query" class="btn btn-primary">查询</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div id="toolbar" class="btn-group">
        <button id="btn_add" type="button" class="btn btn-default">
            <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
        </button>
        <button id="btn_edit" type="button" class="btn btn-default">
            <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
        </button>
        <button id="btn_delete" type="button" class="btn btn-default">
            <span aria-hidden="true"></span>删除
        </button>

        <button id="btn_enter" type="button" class="btn btn-default">
            <span aria-hidden="true" class="glyphicon glyphicon-import"></span>导入
        </button>
        <button id="btn_out" type="button" class="btn btn-default">
            <span aria-hidden="true" class="glyphicon glyphicon-export"></span>导出
        </button>
    </div>
    <table id="tb_tableDemand"></table>
</div>

<div class="modal fade " id="myModalInsert" tabindex="-1" role="dialog" aria-labelledby="myModal" 　
     data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog" role="document" style="width: 40%">
        <div class="modal-content">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h5 class="modal-title" id="exampleModalLabel">新增物料信息</h5>
            </div>

            <div class="modal-body" >
                <form class="form-horizontal" id="sfm" style="margin-left: 14%">
                    <table id="detailList">
                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="name">物料名称:</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="name">
                                <span class="xing"></span>
                            </div>

                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="name">物料型号:</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="materialmodel">
                                <span class="xing"></span>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="profession">颜　　色:</label>

                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="color" >
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="name">包装规格:</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="remark1">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="name">车　　种:</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="remark2">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="profession">计量单位:</label>
                            <div class="col-sm-8">
                                <select name="company" class="form-control">
                                    <option>mg</option>
                                    <option>g</option>
                                    <option>kg</option>
                                    <option>t</option>
                                    <option>斤</option>
                                    <option>公斤</option>
                                    <option>台</option>
                                    <option>个</option>
                                    <option>辆</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="name">单　　价:</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="remark3">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="name">最低安全库存量:</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="remark7">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="name">最高安全库存量:</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="remark8">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="profession">供应商:</label>
                            <div class="col-sm-8">
                                <select name="supplierid" class="form-control txt_search_supper" id="txt_search_supper">
                                </select>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                            <button type="button" class="btn btn-primary" id="submitBtn">保存</button>
                        </div>
                    </table>
                </form>
            </div>
        </div>
    </div>
</div>


<div class="modal fade " id="myModalupdate" tabindex="-1" role="dialog" aria-labelledby="myModal" 　
     data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog" role="document" style="width: 40%">
        <div class="modal-content">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h5 class="modal-title">修改物料信息</h5>
            </div>

            <div class="modal-body" style="margin-left: 14%">
                <form class="form-horizontal" id="update_fm">

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">物料名称:</label>
                        <div class="col-sm-8">
                            <input type="text" class="form-control" name="name">
                            <span class="xing"></span>
                        </div>

                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="materialmodel">物料型号:</label>
                        <div class="col-sm-8">
                            <input type="text" class="form-control" name="materialmodel">
                            <span class="xing"></span>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="colorUpdate">颜　　色:</label>
                        <div class="col-sm-8">
                            <input type="text" class="form-control" id="colorUpdate" name="color" >
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="remark1">包装规格:</label>
                        <div class="col-sm-8">
                            <input type="text" class="form-control" name="remark1">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="remark2">车　　种:</label>
                        <div class="col-sm-8">
                            <input type="text" class="form-control" name="remark2">
                            <input type="hidden" name="id">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="compUpdate">计量单位:</label>
                        <div class="col-sm-8">
                            <select name="company" class="form-control" id="compUpdate">
                                <option>mg</option>
                                <option>g</option>
                                <option>kg</option>
                                <option>t</option>
                                <option>斤</option>
                                <option>公斤</option>
                                <option>台</option>
                                <option>个</option>
                                <option>辆</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="remark3">单　　价:</label>
                        <div class="col-sm-8">
                            <input type="text" class="form-control" name="remark3">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="remark7">最低安全库存量:</label>
                        <div class="col-sm-8">
                            <input type="text" class="form-control" name="remark7">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="remark8">最高安全库存量:</label>
                        <div class="col-sm-8">
                            <input type="text" class="form-control" name="remark8">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="supperUpdate">供应商:</label>
                        <div class="col-sm-8">
                            <select name="supplierid" class="form-control txt_search_supper" id="supperUpdate">
                            </select>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button type="button" class="btn btn-primary" id="submitUpdate">保存</button>
                    </div>

                </form>
            </div>
        </div>
    </div>
</div>


<!--导入数据操作层-->
<%-- <div id="import" class="modal fade bs-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header bg-primary">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                <h4 class="modal-title">文件导入</h4>
            </div>
            <div class="modal-body">
                <div style="text-align:right;padding:5px">
                    <a href="${pageContext.request.contextPath }/datas/Materiel.xls" onclick="javascript:Preview();">
                        <img alt="测试用户信息-模板" src="${pageContext.request.contextPath }/images/excel_alt_1.png" style="width: 20px;"/>
                        <span style="font-size:larger;font-weight:200;color:red">Materiel-模板.xls</span>
                    </a>
                </div>
                <hr/>
                <form id="ffImport" method="post">
                    <div title="Excel导入操作" style="padding: 5px">
                       <input type="hidden" id="AttachGUID" name="AttachGUID" />
                        <input id="excelFile" type="file">
                        <input type="file" id="pic" name="pic">
                    </div>
                </form>

                <!--数据显示表格-->
                <table id="gridImport" class="table table-striped table-bordered table-hover" cellpadding="0" cellspacing="0" border="0">
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="SaveImport()">保存</button>
            </div>
        </div>
    </div>
</div> --%>
<div class="modal fade" id="import" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
   		<div class="modal-dialog" role="document">
    		<div class="modal-content">
      			<div class="modal-header">
        			<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        			<h4 class="modal-title" id="exampleModalLabel">
        				<span style="font-size:13px">excel导入</span>
        			</h4>
      			</div>
      			<div class="modal-body">
	        		<form id="ffImport" method="post" class="form-horizontal">
	          			<div class="form-group">
							<label  class="col-sm-3 control-label"></label>
							<div class="col-sm-7"> 
		           				 <a href="${pageContext.request.contextPath }/datas/Materiel.xls" onclick="javascript:Preview();" id="btn_delete" type="button" class="btn btn-default" onclick="doExportFile()">
					                <span class="glyphicon glyphicon-export" aria-hidden="true"></span>导出
					            </a>
	          				 </div>
	          			</div>
	          			<div class="form-group">
				            <label class="col-sm-5 control-label">excel文件:</label>
				            <div class="col-sm-5"> 
					            <input id="pic" name="pic" type="file" accept=".xls,.xlsx"/>
	          				</div>
	          			</div>
	        			<div class="form-group"> 
				   			<div class="col-sm-3 col-sm-offset-7"> 
						        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
						        <button type="submit"  name="submit"  class="btn btn-primary" id="ss" onclick="SaveImport()">导入</button>
			      			</div>
			      		</div>
	        		</form>
	      		</div>
    		</div>
  		</div>
	</div>


<script type="application/javascript">
    function SaveImport() {
        var ffImport = new FormData($("#ffImport")[0]);

        $.ajax({
            url: "${pageContext.request.contextPath}/materie/materie_import.do",
            type: 'post',
            data: ffImport,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function (data) {
                var result=eval("("+data+")");
                if(result.success){

                    $("#import").modal("hide");
                    $('#tb_tableDemand').bootstrapTable('refresh');
                    
                    var file = doucment.getElementById('pic');
                	file.value = ''; //虽然file的value不能设为有字符的值，但是可以设置为空值
                    
                    toastr.success("操作成功");

                }else{
                    toastr.error('操作失败!');
                    return;
                }
            },
        });
    }
</script>


<script type='text/javascript'>
    var form = $('#sfm');
    $(document).ready(function () {

        formValidator();
    });

    /**
     * 验证方法
     */
    function formValidator() {

        form.bootstrapValidator({
            message: '输入值不合法',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                name: {
                    message: '用户名不合法',
                    validators: {
                        notEmpty: {
                            message: '物料名称不能为空'
                        },
                        stringLength: {
                            min: 1,
                            max: 30,
                            message: '请输入1到30个字符'
                        },
                    }
                }, materialmodel: {
                    message: '用户名不合法',
                    validators: {
                        notEmpty: {
                            message: '物料型号不能为空'
                        },
                        stringLength: {
                            min: 1,
                            max: 30,
                            message: '请输入1到30个字符'
                        },
                        callback: {
                            message: '物料型号不能重复',
                            callback:function(value, validator){

                                var flg = false;

                                $.ajax({
                                    type:'post',
                                    async:false,
                                    data:'materialModel='+value,
                                    url:'${pageContext.request.contextPath}/materie/materie_selectByMaterialmodel.do',
                                    success:function (data) {
                                        var result=eval("("+data+")");
                                        if(result.success){
                                            flg  = true;
                                        }else {
                                            flg = false;
                                        }
                                    }
                                });

                               return flg;


                            }
                        },
                    }
                /* }, remark1: {
                    validators: {
                        notEmpty: {
                            message: '包装规格不能为空'
                        },
                    } */
                }, remark3: {
                    validators: {
                       /*  notEmpty: {
                            message: '单价不能为空'
                        }, */
                        regexp: {
                            regexp: '^(0|[1-9][0-9]{0,9})(\.[0-9]{1,5})?$',
                            message: '格式不正确'
                        }
                    }
                }, remark4: {
                    validators: {
                       /*  notEmpty: {
                            message: '安全库存量不能为空'
                        }, */
                        regexp: {
                            regexp: '^[0-9]*$',
                            message: '安全库存量只能输入整数'
                        }
                    }
                }, remark7: {
                    validators: {
                        notEmpty: {
                            message: '安全库存量不能为空'
                        }, 
                        regexp: {
                            regexp: '^[0-9]*$',
                            message: '安全库存量只能输入整数'
                        }
                    }
                }, remark8: {
                    validators: {
                       /*  notEmpty: {
                            message: '安全库存量不能为空'
                        }, */
                        regexp: {
                            regexp: '^[0-9]*$',
                            message: '安全库存量只能输入整数'
                        }
                    }
                }
            }
        });
    }


    $("#submitBtn").click(function () {
        //进行表单验证
        var bv = form.data('bootstrapValidator');
        bv.validate();
        if (bv.isValid()) {
            //发送ajax请求
            $.ajax({
                url: '${pageContext.request.contextPath }/materie/materieAdd.do',
                type: 'POST',
                data: $('#sfm').serialize(),
                success: function (result) {
                    var resul = eval("(" + result + ")");
                    if (resul.success) {
                        toastr.success("添加成功");
                        $('#myModalInsert').modal('hide');
                        $('#tb_tableDemand').bootstrapTable('refresh');
                    } else {
                        toastr.error('添加失败!');
                    }
                }
            })
        }
    });

    /**
     * 修改物料信息
     */
    $('#submitUpdate').click(function () {

        var bvs = $("#update_fm").data('bootstrapValidator');
        bvs.validate();
        if (bvs.isValid()) {
            $.ajax({
                type: 'post',
                url: '${pageContext.request.contextPath }/materie/Materie_Update.do',
                data: $('#update_fm').serialize(),
                success: function (result) {
                    var resul = eval("(" + result + ")");
                    if (resul.success) {
                        toastr.success("修改成功");
                        $('#myModalupdate').modal('hide');
                        $('#tb_tableDemand').bootstrapTable('refresh');


                        $('#myModalupdate').data('bootstrapValidator').destroy();
                        $('#myModalupdate').data('bootstrapValidator', null);


                    } else {
                        toastr.error('修改失败!');
                    }
                }
            });
        }

    });
</script>

</body>

</html>